<%@ Page Language="C#" MasterPageFile="../Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<OpenQuarters.WebQuarters.Core.CMSPageVersion>" %>
<%@ Import Namespace="OpenQuarters.WebQuarters.Core" %>
<%@ Import Namespace="OpenQuarters.WebQuarters.Utils" %>
<script runat="server">
    
    public string keywords = "";
    public string description = "";
    
    protected override void OnPreRender(EventArgs e)
    {
        keywords = Model.Keywords;
        description = Model.Description;
        
        foreach (ContentAreaDefinition area in Model.CMSPage.CurrentVersion.ContentDefinition.Areas)
        {
            foreach (ModuleDefinition module in area.Modules)
            {
                if (GeneralUtils.GetTypeByName(module.ModuleType) != null && GeneralUtils.GetTypeByName(module.ModuleType.ToString()).GetInterfaces().FirstOrDefault(i => i.Name == "IItemListModule`1") != null)
                {
                    object moduleObj = null;
                    Type moduleType = GeneralUtils.GetTypeByName(module.ModuleType);
                    if (moduleType != null)
                    {
                        try
                        {
                            moduleObj = Activator.CreateInstance(moduleType);
                            typeof(GeneralUtils).GetMethod("FromJSON", new Type[] { typeof(string), typeof(object) }).MakeGenericMethod(moduleType).Invoke(null, new object[] { module.ModuleJSON, moduleObj });

                            IItemListable currentItem = (IItemListable)moduleObj.GetType().GetMethod("CurrentItem").Invoke(moduleObj, new object[] { });

                            if (currentItem != null)
                            {
                                keywords += currentItem.MetaKeywords();
                                description = currentItem.MetaDescription();
                            }

                        }
                        catch (NotImplementedException)
                        {
                        }
                    }
                }
            }
        }
        
    }
    </script>
<asp:Content ID="HeadContent" ContentPlaceHolderID="HeadContent" runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <%
        Console.WriteLine(CMSPage.Current.Keywords);
         %>
    <meta name="keywords" content="<%= keywords %>" />
    <meta name="description" content="<%= description %>" />
    
    <title><%= CMSPage.Current.Title %> - <%= Oracle.GetSettings().SiteTitle %></title>
    
    <link href="/Content/Site.css" rel="stylesheet" type="text/css" />
    <link href="/CMS/File/PageStyleSheets?id=<%= CMSPage.Current.PageVersionId.ToString() %>" rel="stylesheet" type="text/css" />

    <script type="text/javascript" src="/CMS/File/CommonScripts?minify=false"></script>
    <script type="text/javascript" src="/CMS/File/PageScripts?id=<%= CMSPage.Current.PageVersionId.ToString() %>&minify=false"></script>
    <script type="text/javascript">
        <!--
        var cmsPageVersion = $.evalJSON($.base64Decode('<%= OpenQuarters.WebQuarters.Utils.GeneralUtils.Base64Encode(OpenQuarters.WebQuarters.Utils.GeneralUtils.ToJSON(OpenQuarters.WebQuarters.Core.CMSPage.Current)) %>'));
        var cmsCurrentUser = $.evalJSON($.base64Decode('<%= OpenQuarters.WebQuarters.Utils.GeneralUtils.Base64Encode(OpenQuarters.WebQuarters.Utils.GeneralUtils.ToJSON(OpenQuarters.WebQuarters.Core.User.Current)) %>'));
        var cmsIsAuthenticated = <%= OpenQuarters.WebQuarters.Core.User.Current.IsAuthenticated.ToString().ToLower() %>;
        -->
    </script>   
    <script type="text/javascript" src="/CMS/File/AdminScripts?id=<%= CMSPage.Current.PageVersionId.ToString() %>&minify=false"></script>
    <script type="text/javascript" src="/CMS/File/StartupScripts?id=<%= CMSPage.Current.PageVersionId.ToString() %>&minify=false"></script>
    
    <% if (OpenQuarters.WebQuarters.Core.User.Current.HasPermission(OpenQuarters.WebQuarters.Core.Permissions.CMSPage.Edit.SetValue( CMSPage.Current.PageId.ToString())))
   { %>
    <link href="/CMS/File/AdminStyleSheets?id=<%=  CMSPage.Current.PageVersionId.ToString() %>" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="/CMS/File/AdminStartupScripts?id=<%=  CMSPage.Current.PageVersionId.ToString() %>&minify=false"></script>
    <% } %>

    <script src="/Scripts/General.js" type="text/javascript"></script>
</asp:Content>

<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server">
    <% Html.RenderPartial("~/Areas/Site/Views/Templates/" +  CMSPage.Current.Template + ".aspx", ViewData.Model); %>
</asp:Content>
